//
//  HomeTVC.swift
//  微博
//
//  Created by King on 15/9/6.
//  Copyright © 2015年 King. All rights reserved.
//

import UIKit

class HomeTVC: BasicTVC {
    
    
    var statuses:[Status]?{
        didSet{
            tableView.reloadData()
        }
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        if !userAccount.userLogin{
            
            loginView?.setUpViewInfo(true, message: "关注一些人，回这里看看有什么惊喜",image:"visitordiscover_feed_image_smallicon")
            return
        }

        setUpCell()
        loadData()
        
    }
    
    
    
    //MARK: -  加载数据
    func loadData(){
        Status.loadStatus { (dataList, error) -> () in
            if error != nil{
                print(error)
                return
            }
            self.statuses = dataList
         }
            
    }
    //MARK: - cell 设置
    func setUpCell(){
        
        tableView.registerClass(forwordCell.self, forCellReuseIdentifier: satusCellIdentifier.forWordID.rawValue)
        tableView.registerClass(normolCELL.self, forCellReuseIdentifier: satusCellIdentifier.normolID.rawValue)
        //设置预估行高
        tableView.estimatedRowHeight = 300
        //自动计算行高
//        tableView.rowHeight = UITableViewAutomaticDimension
        tableView.separatorStyle = UITableViewCellSeparatorStyle.None
    }
    //MARK: - 数据源方法
    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        
        return statuses?.count  ?? 0
    }
    
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
      
        let status = statuses![indexPath.row]
        let cell = tableView.dequeueReusableCellWithIdentifier(satusCellIdentifier.cellID(status), forIndexPath: indexPath) as! stateCell
        
//        cell.textLabel?.text = statuses![indexPath.row].user?.name
        cell.status = statuses![indexPath.row]
        return cell
    }
    
    override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
        
        let status = statuses![indexPath.row]
        let cell = tableView.dequeueReusableCellWithIdentifier(satusCellIdentifier.cellID(status)) as? stateCell
        
        
        if let h = status.rowHeight{
            return h
        }
        
        status.rowHeight = cell!.rowHeight(status)
        
        return status.rowHeight!
    
    }
    
}



